#!/usr/bin/env python
#Python script to check IPv6 address
#Jay Rao jay.rao@colorado.edu
#version 1.0 2015.03.05
#Copyright (C) 2015 Jay Rao
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.


import sys

def check_ipv6(ip_given):
    """Verify if user entered IPv6 is valid"""
    try:
        list_eachip = list()
        list1 = list()

        if '::' in ip_given:
            list1 = ip_given.split('::')
            if len(list1)>2:
                return False
            else:
                #print list1
                for item in list1:
                    if ':' not in item:
                        list_eachip.append(item)
                    else:
                        a = item.split(':')
                        for j in a:
                            list_eachip.append(j)
                #print list_eachip
                if len(list_eachip)>7:
                    return False
        elif len(ip_given.split(':')) == 8:
            list_eachip = ip_given.split(':')
        else:
            return False


        ipv6 = list()

        for item in list_eachip:
            try:
                i = int(item, 16)
                ipv6.append(i)
            except:
                print("Wrong value for IPv6 address/es")
                sys.exit()

        #print ipv6

        l = len(ipv6)

        if len (list_eachip) > 8 or list_eachip[0] == '0' or list_eachip[l-1] == '0':
            return False

        for ip in ipv6:
            if int(ip) > 65535 or int(ip) < 0:
                return False
        return True

    except():
        print "Wrong value for IPv6 address/es"
        sys.exit()


def main():
    ip = raw_input("Enter IPv6= ")

    value = check_ipv6(ip)

    if value:
        print "Proper IPv6"
    else:
        print "Wrong IPv6"

if __name__ == "__main__":
    main()
